<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{asset('lib/layui-v2.6.3/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/public.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/app.css')}}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo-user">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add-user"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId-user" lay-filter="currentTableFilter-user"></table>

        <script type="text/html" id="currentTableBar-user">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit-user">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete-user">删除</a>
        </script>

    </div>
</div>

<script src="{{asset('lib/layui-v2.6.3/layui.js')}}" charset="utf-8"></script>
<script src="{{asset('lib/jquery-3.4.1/jquery-3.4.1.min.js')}}" charset="utf-8"></script>
<script>
        layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId-user',
            url: '/api/user/index',
            toolbar: '#toolbarDemo-user',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', minWidth: 200, title: '用户名'},
                {field: 'roles', minWidth: 200, title: '角色', templet: returnName},
                {field: 'created_at', width: 200, title: '创建时间'},
                {field: 'updated_at', width: 200, title: '更新时间'},
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar-user', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });
        //返回多级json的值
        function returnName(data) {
            return data.roles[0].name;
        }

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter-user)', function (obj) {
            if (obj.event === 'add-user') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/user/createUser',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        table.on('tool(currentTableFilter-user)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit-user') {
                let index = layer.open({
                    title: '编辑角色',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/user/editRole?id=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete-user') {
                layer.confirm('确定删除吗？', function (index) {
                    $.get({
                        dataType: 'text',
                        url: '/api/user/deleteRole?id=' + obj.data.id,
                        success: function (status) {
                            obj.del();
                        }
                    });
                    layer.close(index);
                });
            }
        });

    });

</script>

</body>
</html>
